/****************************************************
 * Description: DAOimpl for 用户组
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bfsuolframework.security.dao.GroupDAO;
import com.bfsuolframework.security.entity.Group;
import com.bfsuolframework.core.BfsuolConstants;
import com.bfsuolframework.core.dao.HibernateDAOSupport;
import com.bfsuolframework.core.dao.support.HqlParameter;

@Repository
public class GroupDAOImpl extends HibernateDAOSupport<Group> implements GroupDAO {

    //通过 用户主键 查询 用户组 集合
    private final String FIND_GROUP_BYUSERID = "FROM Group group WHERE group.users.id = :userId";
	
	
    /**
     * 通过 用户主键 查询 用户组
     * @return 用户组 集合
     */
    public List<Group> findGroupsByUserId(Long userId){
        return findAll(FIND_GROUP_BYUSERID, HqlParameter.newInstance("userId", userId));
    }


	/* (non-Javadoc)
	 * @see GroupDAO#checkGroupTitle(java.lang.String, java.lang.Long)
	 */
	public boolean checkGroupTitle(String title, Long id) {
		String hql = "select count(*) from Group g where g.title =:title ";
		HqlParameter hqlParameter = HqlParameter.newInstance();

		if (id != null) {
			hql += " and g.id !=:id ";
			hqlParameter.addParameter("id", id);
		}

		int count = getCount(hql, hqlParameter.addParameter("title", title));

		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}


	/* (non-Javadoc)
	 * @see GroupDAO#findAllIsEnabledGroup()
	 */
	public List<Group> findAllIsEnabledGroup() {
		String hql = "FROM Group group WHERE group.isEnabled = :isEnabled";
		return this.findAll(hql, HqlParameter.newInstance("isEnabled", BfsuolConstants.GLOBAL_YESNO_YES));
	}
	

}